package cn.xn.hrms.domain.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.PersonalRewardDTO;
import cn.xn.hrms.api.dto.req.PersonalRewardReqDTO;
import cn.xn.hrms.infrastructure.dataobject.PersonalDO;
import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.exception.BaseException;
import cn.xn.hrms.domain.service.PersonalRewardService;
import cn.xn.hrms.domain.service.PersonalService;
import cn.xn.hrms.infrastructure.dao.PersonalRewardDao;

/**
 * @author ysz
 */
@Service
public class PersonalRewardServiceImpl implements PersonalRewardService {

    @Resource
    PersonalRewardDao personalRewardDao;
    @Resource
    PersonalService personalService;

    @Override
    public PageInfo<PersonalRewardDTO> findAll(int year, int month, Integer departmentId, int personalId, int pageNum,
                                               int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PersonalRewardDTO> personalRewardDTOS = personalRewardDao.selectAll(year, month, departmentId, personalId);
        return new PageInfo<>(personalRewardDTOS);
    }

    @Override
    public List<PersonalRewardDTO> all() {
        return personalRewardDao.selectAll(0, 0, null, 0);
    }

    @Override
    public void insert(PersonalRewardReqDTO personalRewardReqDTO) {
        PersonalDO personalDO = personalService.find(personalRewardReqDTO.getPersonalId());
        PersonalRewardDTO personalRewardDTO = new PersonalRewardDTO();
        BeanUtils.copyProperties(personalRewardReqDTO, personalRewardDTO);
        personalRewardDTO.setPersonalId(personalDO.getPersonalId());
        personalRewardDao.insertSelective(personalRewardDTO);
    }

    @Override
    public void deleteByIdIn(Collection<Integer> idList) {
        personalRewardDao.updateIsDeletedByIdIn(1, idList);
    }

    @Override
    public void updateById(Integer id, PersonalRewardReqDTO personalRewardReqDTO) {
        PersonalRewardDTO personalRewardDTO = personalRewardDao.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(personalRewardDTO)) {
            throw new BaseException(ResultEnum.PERSONAL_REWARD_NOT_EXIST);
        }
        BeanUtils.copyProperties(personalRewardReqDTO, personalRewardDTO);
        PersonalDO personalDO = personalService.find(personalRewardReqDTO.getPersonalId());
        personalRewardDTO.setPersonalId(personalDO.getPersonalId());
        personalRewardDao.updateByPrimaryKeySelective(personalRewardDTO);
    }
}
